import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;

/**
 * @Author: Bink
 * @DateTime: 2021年07月07日 10:33 下午
 * @Description:
 */

public class demo5 {

    private static Connection connection;
    private static Admin admin;
    private static TableName tableName;
    private static Table table;
    private static Scan scan;
    private static SingleColumnValueFilter singleColumnValueFilter;

    public static final String ROW_KEY = "rowKey";
    public static final String COLUMN_FAMILY = "cf";
    public static final String COLUMN_QUALIFIER = "cq";
    public static final String VALUE = "value";
    public static final String TABLE_NAME = "table_name";
    public static final String TARGET = "target";
    public static final String DATE_TIME_STRING = "2019-04-15 16:04:16,513";
    public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss,SSS";

    public static void main(String[] args) throws IOException {
        init();

        // 建表
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
        HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(COLUMN_FAMILY);
        hTableDescriptor.addFamily(hColumnDescriptor);
        if (!admin.tableExists(tableName)) {
            admin.createTable(hTableDescriptor);
        }

        // 删表
        if (admin.tableExists(tableName)) {
            if (admin.isTableEnabled(tableName)) {
                admin.disableTable(tableName);
            }
            admin.deleteTable(tableName);
        }

        // 插值
        Put put = new Put(ROW_KEY.getBytes())
                .addColumn(COLUMN_FAMILY.getBytes(),
                        COLUMN_QUALIFIER.getBytes(),
                        VALUE.getBytes());
        table.put(put);
        // put batch
        table.put(Arrays.asList(put, put, put));

        // 查值
        Get get = new Get(ROW_KEY.getBytes())
                .addColumn(COLUMN_FAMILY.getBytes(),
                        COLUMN_QUALIFIER.getBytes());
        Result result = table.get(get);
        // get batch
        table.get(Arrays.asList(get, get, get));

        // 扫描
        ResultScanner scanner = table.getScanner(scan);
        // todo

        // 过滤
        Scan sf = scan.setFilter(singleColumnValueFilter);
        ResultScanner resultScanner = table.getScanner(sf);
        // todo
        Get gf = get.setFilter(singleColumnValueFilter);
        Result result1 = table.get(gf);

        // release
        release();
    }

    private static void release() throws IOException {
        table.close();
        admin.close();
        connection.close();
    }

    private static void init() throws IOException {
        connection = ConnectionFactory.createConnection();
        admin = connection.getAdmin();
        tableName = TableName.valueOf(TABLE_NAME);
        table = connection.getTable(tableName);
        scan = new Scan();
        singleColumnValueFilter = new SingleColumnValueFilter(COLUMN_FAMILY.getBytes(),
                COLUMN_QUALIFIER.getBytes(),
                CompareOperator.EQUAL,
                TARGET.getBytes());
    }

    static void sdf() throws ParseException {
        String dateTimeString = DATE_TIME_STRING;

        LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS_SSS));
        long millis = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        long seconds = localDateTime.toEpochSecond(ZoneOffset.ofHours(8));
//        1555315456513
//        1555315456

        Date parse = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_SSS).parse(dateTimeString);
        long parseTime = parse.getTime();
//        1555315456513
    }
}
